/*----------------------------MegaWave2 module----------------------------*/
/* mwcommand
name = {movExtractLine};
version = {"1.0"};
author = {"Baptiste Coulange"};
function = {"Extrait les niveaux de gris correspondant au pixel sur chaque image de la movie"};
usage = {
in->in				"Fmovie input",
px->px           "row of the pixel",
py->py           "line of the pixel",
'f':[fx=0]->fx	"Demi taille de la fenetre de moyennage (dim x)",
'F':[fy=0]->fy	"Demi taille de la fenetre de moyennage (dim y)",
out<-out         "output Fsignal",
};
*/
/*----------------------------------------------------------------------
v1.0 (08/2008): initial version
----------------------------------------------------------------------*/

#include <stdio.h>
#include <math.h>
#include "mw.h"

void movExtractLine(in,px,py,out,fx,fy)
   Cmovie in;
   int px,py;
   Fsignal out;
   int *fx,*fy;
{
 	Cimage Loc;
	int taille,valL;
	taille=1;
	valL=0;
	Loc=in->first;
	
	int norm;
	norm=(2*(*fx)+1)*(2*(*fy)+1);
	 while(Loc->next){
		Loc=Loc->next;
		taille=taille+1;
	}
	Loc=in->first;
	
	out=mw_change_fsignal(out,taille);
	int i;
	int j;
	int normR;
	normR=norm;
	for (i=0;i<*fx+1;i++)
		for(j=0;j<*fy+1;j++) {
			if((px+i<(Loc->nrow))&&(py+j<(Loc->ncol))){
				out->values[0]=out->values[0]+Loc->gray[(px+i)*(Loc->ncol)+(py+j)];  
				} else { 
			if((px+i<(Loc->nrow))&&(py-j>0)){
				out->values[0]=out->values[0]+Loc->gray[(px+i)*(Loc->ncol)+(py-j)];  
				} else {			
			if((px-i>0)&&(py+j<(Loc->ncol))){
				out->values[0]=out->values[0]+Loc->gray[(px-i)*(Loc->ncol)+(py+j)];  
				} else {
			if((px-i>0)&&(py-j>0)){
				out->values[0]=out->values[0]+Loc->gray[(px-i)*(Loc->ncol)+(py-j)];  
				} else { normR=normR-1;}			
	}}}}
	
	out->values[0]=(double)out->values[0]/(double)normR;

		while(Loc->next){
			Loc=Loc->next;
			valL=valL+1;
			normR=norm;
			
			for (i=0;i<*fx+1;i++)
				for(j=0;j<*fy+1;j++) {
					if((px+i<(Loc->nrow))&&(py+j<(Loc->ncol))){
					out->values[valL]=out->values[valL]+Loc->gray[(px+i)*(Loc->ncol)+(py+j)];  
					} else {
					if((px+i<(Loc->nrow))&&(py-j>0)){
					out->values[valL]=out->values[valL]+Loc->gray[(px+i)*(Loc->ncol)+(py-j)];  
					} else {			
					if((px-i>0)&&(py+j<(Loc->ncol))){
					out->values[valL]=out->values[valL]+Loc->gray[(px-i)*(Loc->ncol)+(py+j)];  
					} else {
					if((px-i>0)&&(py-j>0)){
					out->values[valL]=out->values[valL]+Loc->gray[(px-i)*(Loc->ncol)+(py-j)];  
					} else { normR=normR-1;}			
			}}}}

				out->values[valL]=(double)out->values[valL]/(double)normR;
		}
			out->scale=1.0;
			sprintf(out->cmt, "le pixel %d,%d à travers les échelles", px, py);
	
	
}
